package TASKey;

import java.util.Scanner;

public class TASKey {

	private static final String ADD_ERROR = "add error";
	private static final String ADD_SUCCESS = "add a new task!";
	private static final String WRONG_COMMAND = "wrong command!";
	private static final String NO_TASK = "no task";

	// task[i][j]: i: the number of task j: 0->Date & Month 1->Time
	// 2->Description
	// if task doesn't have Date&Month, task[i][0] = " "
	public static String[][] task = new String[50][3];
	public static int taskNumber = -1;

	private static Scanner scanner = new Scanner(System.in);

	/**
	 * Judge if c is a digit char
	 * 
	 * @param c
	 * @return
	 */
	public static boolean isDigit(char c) {
		if ((c >= '0') && (c <= '9')) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * get the description part from strArray
	 * 
	 * @param a
	 * @param b
	 * @param strArray
	 * @return String description
	 */
	public static String getDescription(int a, int b, String[] strArray) {
		String result = strArray[a];
		int i;
		if (a != b) {
			for (i = a + 1; i <= b; i++) {
				result = result + " " + strArray[i];
			}
		}

		return result;
	}

	/**
	 * This is the add method. It can judge which kind of task users want to
	 * add.
	 * 
	 * @param strArray
	 *            the splited cmd It's a string array.
	 * @return feedback of add
	 */
	public static String add(String[] strArray) {
		if (!(isDigit(strArray[1].toCharArray()[0]))) {
			return addTask(" ", " ",
					getDescription(1, strArray.length - 1, strArray));
		} else if ((isDigit(strArray[1].toCharArray()[0]))
				&& (!(isDigit(strArray[3].toCharArray()[0])))) {
			return addTask(strArray[1] + " " + strArray[2], " ",
					getDescription(3, strArray.length - 1, strArray));
		} else if (strArray[4].equals("-")) {
			return addTask(strArray[1] + " " + strArray[2], strArray[3] + " "
					+ strArray[4] + " " + strArray[5],
					getDescription(6, strArray.length - 1, strArray));
		} else if ((isDigit(strArray[1].toCharArray()[0]))
				&& (isDigit(strArray[3].toCharArray()[0]))) {
			return addTask(strArray[1] + " " + strArray[2], strArray[3],
					getDescription(4, strArray.length - 1, strArray));
		} else {
			return ADD_ERROR;
		}
	}

	/**
	 * add a new task. If the task has no time, String time = " ". If the task
	 * has no date, String date = " "
	 * 
	 * @param date
	 * @param time
	 * @param description
	 * @return feedback of add
	 */
	public static String addTask(String date, String time, String description) {
		taskNumber++;
		task[taskNumber][0] = date;
		task[taskNumber][1] = time;
		task[taskNumber][2] = description;
		return ADD_SUCCESS;
	}

	public static String executeCommand(String cmd) {
		String[] cmdArray = cmd.split(" ");
		if (cmdArray[0].equals("Add")) {
			return add(cmdArray);
		} else {
			return WRONG_COMMAND;
		}
	}

	/**
	 * combine date&month,time,description to one string This method is just for
	 * testing. Maybe we don't need this method in future work
	 * 
	 * @param strArray
	 * @return
	 */
	public static String getDisplay(String[] strArray) {
		String result = "";
		for (int i = 0; i <= 2; i++) {
			if (strArray[i].equals(" ")) {
				continue;
			} else {
				result = result + strArray[i] + " | ";
			}
		}
		return result;
	}

	/**
	 * display all tasks. It can show the division of date,time,description.
	 */
	public static void display() {
		if (taskNumber == -1) {
			System.out.println(NO_TASK);
		} else {
			for (int i = 0; i <= taskNumber; i++) {
				String number = (i + 1) + "";
				System.out.println(number + "." + "  " + getDisplay(task[i]));
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		while (true) {
			display();
			System.out.print("Enter command:");
			String command = scanner.nextLine();
			String userCommand = command;
			String feedback = executeCommand(userCommand);
			System.out.println(feedback);
		}
	}

}
